<?php 
/////////////////////////////////////////////////////////////////////////////
// 这个文件是 网城创想多用户商城 项目的一部分
//
// Copyright (c) 2007 - 2008 www.shopnc.net
//
// 要查看完整的版权信息和许可信息，请查看源代码中附带的 COPYRIGHT 文件，
// 或者访问 http://www.shopnc.net/ 获得详细信息。
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : product.manage.php   FILE_PATH : member\own_order.php
 * ....商品订单管理功能
 *
 * @copyright Copyright (c) 2007 - 2007 www.shopnc.net 
 * @author 网城创想多用户商城开发团队 php_netproject@yahoo.com.cn
 * @package 
 * @subpackage 
 * @version Tue Aug 28 15:51:41 CST 2007
 */
require_once("../global.inc.php");

class OwnProductOrderManage extends CommonFrameWork{
	/**
	 * 商品订单对象
	 *
	 * @var obj
	 */
	var $obj_product_order;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	/**
	 * 会员对象
	 *
	 * @var obj
	 */
	var $obj_member;
	/**
	 * 商品对象
	 *
	 * @var obj
	 */
	var $obj_product;
	/**
	 * 网站提醒对象
	 *
	 * @var obj
	 */
	var $obj_remind;
	
	function main(){
		/**
		 * 创建商品订单对象
		 */
		if (!is_object($this->obj_product_order)){
			require_once("order.class.php");
			$this->obj_product_order = new ProductOrderClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->objvalidate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}

		/**
		 * 设置模板路径
		 */
		$this->setsubtemplates("member");

		/**
		 * 语言包
		 */
		$this->getlang("product");

		$this->memberMenuModule();

		switch ($this->_input['action']){
			case "bought":
				$order_case = "bought";
				$this->_listorder($order_case);
				break;
			case "group":
				$group_state = "group_buying";
				$this->_listgroup($group_state);
				break;
			case "group_end":
				$group_state = "group_end";
				$this->_listgroup($group_state);
				break;
			case "sold":
				$order_case = "sold";
				$this->_listorder($order_case);
				break;
			case "payed":
				$this->_updateproductorderstate(1);
				break;
			case "delived":
				$this->_updateproductorderstate(2);
				break;
			case "received":
				$this->_updateproductorderstate(3);
				break;
			default:
				$order_case = "bought";
				$this->_listorder($order_case);
				break;
		}

	}

	/**
	 * 取得订单列表
	 *
	 * @param string $order_case 
	 */
	function _listorder($order_case){

		/**
		 * 初始化分页类
		 */
		require_once("commonpage.class.php");
		$obj_page = new CommonPage();

		/**
		 * 实例化用户类
		 */
		if (!is_object($this->obj_member)){
			require_once("member.class.php");
			$this->obj_member = new MemberClass();
		}
		/**
		 * 取得查询参数
		 */
		$obj_condition[key] = $this->_input['keyword'];
		$obj_condition[keygenre] = $this->_input['searchtype'];
		$obj_condition[time] = $this->_input['time'];
		$obj_condition[p_name] = $this->_input['p_name'];
		/*如果有卖家名称*/
		if ($this->_input['seller_nick'] != "" ){
			$condition[member_name] = $this->_input['seller_nick'];
			$member_info = $this->obj_member->getMemberInfo($condition);
			if (!empty($member_info)){
				$obj_condition[seller_id] = $member_info['member_id'];
			}
			unset($condition);
		}
		/*时间搜索*/
		if ($this->_input['start_time'] != "" && $this->_input['end_time'] != "" ){
			$time = @explode('-',$this->_input['start_time']);
			$start_time = @mktime(0,0,0,$time[1],$time[2],$time[0]);
			$time = @explode('-',$this->_input['end_time']);
			$end_time = @mktime(0,0,0,$time[1],$time[2],$time[0]);
			$obj_condition[search_time] = 1;
		}
		/**
		 * 其他限定条件
		 */
		$obj_condition[state] = array("0","1","2","3");
		$obj_condition[member_id] = $_SESSION['s_login']['id'];
		$obj_condition[order_case] = $order_case;

		/**
		 * 取得订单列表
		 */
		$obj_page->pagebarnum(10);
		$product_order_array = $this->obj_product_order->getProductOrderList($obj_condition, $obj_page);
		if(is_array($product_order_array)){
			foreach ($product_order_array as $key => $value){
				if($order_case == "bought"){
					$member_id = $value['seller_id'];
				}else{
					$member_id = $value['buyer_id'];
				}
				$condition[id] = $member_id;
				$member_info = $this->obj_member->getMemberInfo($condition);
				$product_order_array[$key]['member_nick'] = $member_info['login_name'];
				$product_order_array[$key]['member_id'] = $member_id;

				if($product_order_array[$key]['sold_have_comment'] == "0" && $product_order_array[$key]['buy_have_comment'] == "0"){
					if ($product_order_array[$key]['sp_state'] == "3"){
						$product_order_array[$key]['score_say'] = '<a href="own_score.php?action=add&orderid=' . $product_order_array[$key]['sp_id'] . '&type=' . $this->_input['action'] . '">未评价';
					}else{
						$product_order_array[$key]['score_say'] = '未评价';
					}

				}else if($product_order_array[$key]['sold_have_comment'] == "1" && $product_order_array[$key]['buy_have_comment'] == "1"){
					$product_order_array[$key]['score_say'] = '双方已评价';
				}else if($this->_input['action'] == "sold"){
					if ($product_order_array[$key]['buy_have_comment'] == "1"){
						$product_order_array[$key]['score_say'] = '对方已评价';
					}else{
						$product_order_array[$key]['score_say'] = '对方未评价';
					}
					if ($product_order_array[$key]['sole_have_comment'] == "0"){
						if ($product_order_array[$key]['sp_state'] == "3"){
							$product_order_array[$key]['score_say'] .= '<br><a href="own_score.php?action=add&orderid=' . $product_order_array[$key]['sp_id'] . '&type=' . $this->_input['action'] . '">未评价';

						}
					}
				}else if($this->_input['action'] == "bought"){
					if ($product_order_array[$key]['sole_have_comment'] == "1"){
						$product_order_array[$key]['score_say'] = '对方已评价';
					}else{
						$product_order_array[$key]['score_say'] = '对方未评价';
					}
					if ($product_order_array[$key]['buy_have_comment'] == "0"){
						if ($product_order_array[$key]['sp_state'] == "3"){
							$product_order_array[$key]['score_say'] .= '<br><a href="own_score.php?action=add&orderid=' . $product_order_array[$key]['sp_id'] . '&type=' . $this->_input['action'] . '">未评价';

						}

					}
				}

			}
		}

		$page_list = $obj_page->show(1);

		/**
		 * 页面输出
		 */
		$this->output("member_id", $_SESSION['s_login']['id']);
		$this->output("order_case", $order_case);
		$this->output("page_list", $page_list);
		$this->output("time", $this->_input['time']);
		$this->output("product_order_array", $product_order_array);
		$this->showpage("own_order.manage");
	}
	/**
	 * 团购商品列表
	 *
	 */
	function _listgroup($group_state){

		/**
		 * 初始化分页类
		 */
		require_once("commonpage.class.php");
		$obj_page = new CommonPage();

		/**
		 * 实例化用户类
		 */
		if (!is_object($this->obj_member)){
			require_once("member.class.php");
			$this->obj_member = new MemberClass();
		}
		/**
		 * 实例化商品类
		 */
		if (!is_object($this->obj_product)){
			require_once("product.class.php");
			$this->obj_product = new ProductClass();
		}
		/**
		 * 取得查询参数
		 */
		$obj_condition[key] = $this->_input['keyword'];
		$obj_condition[keygenre] = $this->_input['searchtype'];

		/**
		 * 其他限定条件
		 */
		$obj_condition[order_case] = "bought";
		if($group_state == "group_buying"){
			$obj_condition[state] = array("4");
			$obj_condition[sell_type] = "2";
		}else{
			$obj_condition[state] = array("0", "5");
			$obj_condition[sell_type] = "2";
		}

		$obj_condition[member_id] = $_SESSION['s_login']['id'];
		$obj_condition[order_case] = $order_case;

		/**
		 * 更新到期团购商品订单状态
		 */
		$group_product_order_tobe_end_array = $this->obj_product_order->updateProductOrderInCondition();
		/**
		 * 更新到期商品状态
		 */
		$product_tobe_end_array = $this->obj_product->updateProductInCondition();
		/**
		 * 更新过期交易结束团购未成立的订单状态
		 */


		/**
		 * 取得订单列表
		 */
		$obj_page->pagebarnum(10);
		$product_order_array = $this->obj_product_order->getProductOrderList($obj_condition, $obj_page);
		if(is_array($product_order_array)){
			foreach ($product_order_array as $key => $value){
				if($order_case == "bought"){
					$member_id = $value['seller_id'];
				}else{
					$member_id = $value['buyer_id'];
				}
				$member_info = $this->obj_member->getMemberInfo($member_id);
				$product_order_array[$key]['member_nick'] = $member_info['login_name'];
				$product_order_array[$key]['member_id'] = $member_id;

				$product_info = $this->obj_product->getProductRow($value['p_code']);
				if($product_info['p_sold_num'] < $product_info['p_group_mincount']){
					$product_order_array[$key]['less_count'] = $product_info['p_group_mincount'] - $product_info['p_sold_num'];
				}
				$left_time = $product_info['p_end_time'] - time();
				if($left_time < 0){
					$is_end = "1";
				}
				$product_order_array[$key]['left_days'] = intval($left_time / (24*60*60));
				$product_order_array[$key]['left_hours'] = intval(($left_time % (24*60*60)) / (60*60));
				$product_order_array[$key]['left_minutes'] = intval((($left_time % (60*60))) / 60);

			}
		}
		if(count($product_order_array)<1){
			$no_order = "no_order";
		}

		$page_list = $obj_page->show(1);
		//		print_r($product_order_array);
		/**
		 * 页面输出
		 */
		$this->output("member_id", $_SESSION['s_login']['id']);
		$this->output("order_case", $order_case);
		$this->output("no_order", $no_order);
		$this->output("is_end", $is_end);
		$this->output("page_list", $page_list);
		$this->output("product_order_array", $product_order_array);
		$this->showpage("own_group_order.manage");
	}


	/**
	 * 更改商品订单状态
	 *
	 * @param int $state 0:已成交 1:已支付 2:已发货 3:已收货
	 */
	function _updateproductorderstate($state){
		/**
		 * 验证提交信息
		 */
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["spcode"],"require"=>"true","message"=>$this->_lang['errProductCodeEmpty']));
		$error = $this->obj_validate->validate();
		if($error != ""){
			echo $error;
		}else{
			if ($state == 2){/*卖家发货，提醒买家*/
				/**
				 * 网站提醒操作
				 */
				if (!is_object($this->obj_remind)){
					require_once('remind.class.php');
					$this->obj_remind = new RemindClass();
				}
				/**
				 * 实例化用户类
				 */
				if (!is_object($this->obj_member)){
					require_once("member.class.php");
					$this->obj_member = new MemberClass();
				}
				$condition_order['sp_code'] = $this->_input["spcode"];
				$product_array = $this->obj_product_order->getProductOrderList($condition_order,&$obj_page);

				$condition['id'] = $product_array[0]['buyer_id'];
				$member_array = $this->obj_member->getMemberInfo($condition);
		
				$value_array = array();
				$value_array['username'] = $member_array['login_name'];
				$value_array['product_name'] = $product_array[0]['p_name'];
				$this->obj_remind->setMessageOrMail('buyer_sale_to_succ','sale_to_succ',$value_array,$member_array['login_name'],$this->_configinfo);
				
			}
			
			
			$this->_input["txtSPstate"] = $state;
			$result = $this->obj_product_order->updateProductOrderState($this->_input);
			echo $this->_lang['alertProductOrderStateOk'];
		}
	}
}

$product_order_manage = new OwnProductOrderManage();
$product_order_manage->main();
unset($product_order_manage);
?>